package com.siqi.hr.common.entity;

import com.siqi.hr.common.utils.json.JSONArray;
import com.siqi.hr.common.utils.json.JSONException;
import com.siqi.hr.common.utils.json.JSONObject;
import com.siqi.hr.system.common.chan.TSQLSelectStat;
import com.siqi.hr.system.common.sql.Condition;
import com.siqi.hr.system.common.sql.SField;
import com.siqi.hr.system.common.sql.STable;
import com.siqi.hr.system.common.utils.Common;
import org.apache.commons.lang.StringUtils;

import java.util.*;

/**
 * 封装导入导出数据对象
 * @author     xiaogq
 * @date       2017-01-05
 */
public class ExportEntity extends TSQLSelectStat {

    /**
     * 实体id
     */
    private int entityId;

    /**
     * 导出文件格式(1:*.xls,2:*.xlsx,3:*.csv)
     */
    private int kind;

    /**
     * 如果导出csv格式文件分隔符
     */
    private String splitValue;

    /**
     * 实体记录id过滤条件
     */
    private String searchIds;

    /**
     * 编码格式
     */
    private String unicode;

    /**
     * 导出实体字段信息
     */
    private String jsonChar;

    /**
     * 搜索条件
     */
    private Set<Condition> mutilSearch = new HashSet<Condition>();

    /**
     * 实体记录是否是有效记录(true:是,false:否)
     */
    private boolean validFlag = true;

    /**
     * 导出文件存储url地址
     */
    private String url;

    public String getJsonChar() {
        return jsonChar;
    }

    public void setJsonChar(String jsonChar) {
        this.jsonChar = jsonChar;
    }

    public int getKind() {
        return kind;
    }

    public void setKind(int kind) {
        this.kind = kind;
    }

    public String getSplitValue() {
        return splitValue;
    }

    public void setSplitValue(String splitValue) {
        this.splitValue = splitValue;
    }

    public String getUnicode() {
        return unicode;
    }

    public void setUnicode(String unicode) {
        this.unicode = unicode;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public List<SField> getFieldChan(String fieldJson, Map<String, Object> celMap) {
        STable mainTable = this.tableChan.getMianTable();
        List sf = new ArrayList();
        try{
            JSONArray jsArray = new JSONArray(fieldJson);
            for (int i = 0; i < jsArray.length(); i++) {
                JSONObject jo = (JSONObject)jsArray.get(i);
                celMap.put(jo.getString("fieldName"), jo.getString("inchinese"));
                String fieldName = jo.getString("fieldName");

                if (StringUtils.contains(fieldName, "：")) {
                    String[] fieldArray = fieldName.split("：");
                    String mainFieldName = fieldArray[0];
                    String quoteFieldName = fieldArray[1];
                    String byName = mainFieldName + "___" + quoteFieldName;
                    STable st = new STable(Common.getFkTableNameByField(mainFieldName), mainFieldName, mainTable.getByName());
                    this.tableChan.add(st);
                    if (SField.isSelectBox(quoteFieldName)) {
                        st = new STable("sys_custom_selectbox", quoteFieldName, mainFieldName);
                        if (fieldName.endsWith("_mutibox")) {
                            sf.add(new SField(quoteFieldName, st, byName));
                        }else {
                            sf.add(new SField("name", st, byName));
                        }
                        this.tableChan.add(st);
                    }else if (quoteFieldName.endsWith("_entity")) {
                        st = new STable("sys_custom_entity", quoteFieldName, mainFieldName);
                        sf.add(new SField("inchinese", st, byName));
                        this.tableChan.add(st);
                    }else if (quoteFieldName.endsWith("_object")) {
                        sf.add(new SField("hideInchinese_varchar", new STable(Common.getFkTableNameByField(mainFieldName)), byName));
                    }else if (quoteFieldName.endsWith("_bit")) {
                        SField field = new SField(quoteFieldName, new STable(mainFieldName), byName);
                        field.setFuction("if(@ is true, '是', '否')");
                        sf.add(field);
                    } else {
                        sf.add(new SField(quoteFieldName, st, byName));
                    }
                }else if (fieldName.startsWith("pk_")){
                    STable st = new STable(Common.getFkTableNameByField(fieldName), fieldName, mainTable.getByName());
                    if ((fieldName.startsWith("pk_sys_custom_entity_")) || (fieldName.startsWith("pk_sys_custom_entity_type_"))) {
                        sf.add(new SField("inchinese", st, fieldName));
                    }else {
                        sf.add(new SField("name_varchar", st, fieldName));
                    }
                    this.tableChan.add(st);
                }else if (SField.isSelectBox(fieldName)){
                    if (fieldName.endsWith("_mutibox")) {
                        STable st = new STable("sys_custom_selectbox", fieldName, mainTable.getByName());
                        sf.add(new SField(fieldName, mainTable, fieldName));
                        this.tableChan.add(st);
                    } else {
                        STable st = new STable("sys_custom_selectbox", fieldName, mainTable.getByName());
                        sf.add(new SField("name", st, fieldName));
                        this.tableChan.add(st);
                    }
                }else if (fieldName.endsWith("_entity")){
                    STable st = new STable("sys_custom_entity", fieldName, mainTable.getByName());
                    sf.add(new SField("inchinese", st, fieldName));
                    this.tableChan.add(st);
                }else if (fieldName.endsWith("_object")) {
                    sf.add(new SField("hideInchinese_varchar", mainTable, fieldName));
                }else if (fieldName.endsWith("_bit")) {
                    SField field = new SField(fieldName, mainTable, fieldName);
                    if(fieldName.equals("flag_bit")){
                        field.setFuction("if(@ is true,  '停用', '启用')");
                    }else{
                        field.setFuction("if(@ is true, '是', '否')");
                    }
                    sf.add(field);
                }else {
                    sf.add(new SField(fieldName, mainTable, fieldName));
                }
            }
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        return sf;
    }

    public Set<Condition> getMutilSearch() {
        return mutilSearch;
    }

    public void setMutilSearch(Set<Condition> mutilSearch) {
        this.mutilSearch = mutilSearch;
    }

    public boolean isValidFlag() {
        return validFlag;
    }

    public void setValidFlag(boolean validFlag) {
        this.validFlag = validFlag;
    }

    public int getEntityId() {
        return entityId;
    }

    public void setEntityId(int entityId) {
        this.entityId = entityId;
    }

    public String getSearchIds() {
        return searchIds;
    }

    public void setSearchIds(String searchIds) {
        this.searchIds = searchIds;
    }
}
